package com.zzh.mvvm.binding.click;


import static com.zzh.mvvm.binding.adapter.ViewAdapter.CLICK_INTERVAL;

import android.annotation.SuppressLint;
import android.view.View;

import com.zzh.mvvm.binding.BindingAction;
import com.zzh.mvvm.binding.click.observable.ViewClickObservable;
import com.zzh.mvvm.binding.click.observable.ViewLongClickObservable;

import java.util.concurrent.TimeUnit;

import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.functions.Consumer;

/**
 * Created by zzh on 2023/11/1.
 *
 * @date: 2023/11/1 20:47
 * @email: zzh_hz@126.com
 * @author: zzh
 * @description: com.zzh.mvvm.binding.click
 */
public class RxViews {

    /**
     * 点击时间
     *
     * @param view
     * @return
     */
    public static Observable<Object> clicks(View view) {
        return new ViewClickObservable(view);
    }

    /**
     * 防抖点击事件
     *
     * @param view        点击的View，
     * @param clickAction 点击事件
     */
    @SuppressLint("CheckResult")
    public static void clickAntiShake(View view, BindingAction clickAction) {
        RxViews.clicks(view)
                .throttleFirst(CLICK_INTERVAL, TimeUnit.SECONDS)//1秒钟内只允许点击1次
                .subscribe((Consumer) object -> {
                    if (clickAction != null) {
                        clickAction.call();
                    }
                });
    }

    public static Observable<Object> longClicks(View view) {
        return new ViewLongClickObservable(view, () -> true);
    }

    public static Observable<Object> longClicks(View view, ViewLongClickObservable.OnHandledCallback callback) {
        return new ViewLongClickObservable(view, callback);
    }
}
